/*
 * OptionGui.java
 */

/* Copyright information:
 * 
 *  Copyright 2010 Failboat Productions. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY FAILBOAT PRODUCTIONS ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FAILBOAT PRODUCTIONS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Failboat Productions.
 */

package GUI;

import com.trolltech.qt.core.QRect;
import com.trolltech.qt.core.QSize;
import com.trolltech.qt.core.Qt.Orientation;
import com.trolltech.qt.gui.QCheckBox;
import com.trolltech.qt.gui.QCloseEvent;
import com.trolltech.qt.gui.QColor;
import com.trolltech.qt.gui.QComboBox;
import com.trolltech.qt.gui.QDialog;
import com.trolltech.qt.gui.QFrame;
import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QPalette;
import com.trolltech.qt.gui.QPushButton;
import com.trolltech.qt.gui.QRadioButton;
import com.trolltech.qt.gui.QSlider;
import com.trolltech.qt.gui.QStackedWidget;
import com.trolltech.qt.gui.QTreeWidget;
import com.trolltech.qt.gui.QTreeWidgetItem;
import com.trolltech.qt.gui.QWidget;
import com.trolltech.qt.gui.QPalette.ColorRole;

/**
 * 
 * @author bj0rns0der
 * @version 1.0 BETA
 */
public class OptionGui extends QDialog {
	private String boardColor;
	private String boardSheet;
	private String hudcolor;
	private String iconTypeString;
	private String selectionColor;
	private String styleSheet;
	private final String IMAGEPATH="border-image: url(bin/GUI/img_icons/pieces/00b_bishop.png); border: none;";
    private int blue;
    private int gameMode;
    private int green;
    private int newX;
    private int minScroll = 0;
    private int maxScroll=255;
    private int red;
    private boolean modeChange=false;
    private boolean newTimerChoice;
    private boolean oldTimerChoice;
    private boolean newGame;
    private boolean oldNewGame;
	private ChessGUI gui;
	private Settingshandler handler;
	private QCheckBox quickChess;
	private QCheckBox timer;
	private QComboBox resComboBox;
	private QComboBox selComboBox;
	private QComboBox boardComboBox;	
	private QDialog mainWindow;
	private QFrame hudPageColorPrev;
	private QLabel hudColorLab;
	private QLabel boardLabel;
	private QLabel colorsmain;
	private QLabel gfxLabel;
	private QLabel resolutionLabel;
	private QLabel generalSettingsLab;
	private QLabel gameModeLab;
    private QLabel iconLabel;
    private QLabel redLabel;
    private QLabel greenLabel;
    private QLabel blueLabel;
    private QLabel warningLab;
    private QLabel selectionLabel;
    private QPushButton cancelButton;
    private QPushButton okButton;
    private QPushButton resetButton;
    private QPushButton iconModern;
    private QPushButton iconClassic;
    private QPushButton iconPlastic;
    private QPushButton iconWood;
    private QPushButton iconOld;
    private QPushButton iconCurrent;
    private QPushButton boardCurrent;
    private QPushButton iconPaper;
    private QRadioButton standardGM;	
    private QRadioButton lockedGM;
    private QRadioButton noPawnsGM;
    private QRadioButton HaxxorGM;
    private QPalette pal;				
    private QStackedWidget mainFrame;	
    private QSlider redSlider;			
	private QSlider greenSlider;
	private QSlider blueSlider;
    private QTreeWidget treeWidget; 
    private QTreeWidgetItem colors; 
    private QTreeWidgetItem selection;
    private QTreeWidgetItem board;
    private QTreeWidgetItem hud;
    private QTreeWidgetItem gfx;
    private QTreeWidgetItem resolution;
    private QTreeWidgetItem generalSettings;
    private QTreeWidgetItem iconType;
    private QWidget colorsMainPage;
	private QWidget iconPage;
	private QWidget gfxPage;
	private QWidget hudPage;
	private QWidget boardPage;
	private QWidget generalSettingsPage;
    private QWidget selectionPage;
	private QWidget resolutionPage;
 
    
    OptionGui(ChessGUI gui) {
    	this.gui=gui;
    	if(gui.isClock()){
    		gui.stopClock();
    	}
    	setupUi(this);
    	handler = new Settingshandler();
    	handler.loadSettings();
    	newX=handler.getChessboardSizeX();
    	boardColor=handler.getBackgroundtype();
    	iconTypeString=handler.getIcontype();
    	hudcolor=handler.getHudcolor();
    	selectionColor=handler.getSelectcolor();
    	gameMode=handler.getGameMode();
    }
    
    /**
     * Overrides default close event to resume clock when window is closed
     */
    protected void closeEvent(QCloseEvent e){
    	if(gui.isClock()){
    		gui.resumeClock();
    	}
    }
    
    /**
     * Adds all the widgets/frames in the options gui.
     * @param mainWindow
     */
    private void setupUi(QDialog mainWindow){
    	this.mainWindow=mainWindow;    	
    	//sets the variables needed for setting icon,countDown,styleSheet,boardSheet and sliders.
    	handler = new Settingshandler();
    	handler.loadSettings();
    	sortColors();
    	gameMode=handler.getGameMode();
        oldTimerChoice=handler.getCountDown();
        oldNewGame=handler.getQuick();
    	iconTypeString=handler.getIcontype();
    	boardColor=handler.getBackgroundtype();
    	styleSheet ="border-image: url(bin/GUI/img_icons/pieces/"+iconTypeString+"b_bishop.png); border: none;";
        boardSheet ="border-image: url(bin/GUI/img_icons/background"+boardColor+".png); border: none;";
        mainWindow.setObjectName("Options");
        mainWindow.resize(new QSize(586, 414).expandedTo(mainWindow.minimumSizeHint()));
        mainFrame = new QStackedWidget(mainWindow);
        mainFrame.setObjectName("mainFrame");
        mainFrame.setGeometry(new QRect(190, 10, 361, 351));
        mainFrame.setWindowIcon(new QIcon(IMAGEPATH+"about.png"));
    	mainFrame.setWindowTitle("About Chess");
        //colorsmain
        colorsMainPage = new QWidget();
        colorsMainPage.setObjectName("colorsMainPage");
        colorsmain = new QLabel(colorsMainPage);
        colorsmain.setObjectName("colorsmain");
        colorsmain.setGeometry(new QRect(30, 31, 281, 31));
        mainFrame.addWidget(colorsMainPage);
        //boardPage
        boardPage = new QWidget();
        boardPage.setObjectName("boardPage");
        boardLabel = new QLabel(boardPage);
        boardLabel.setObjectName("boardLabel");
        boardLabel.setGeometry(new QRect(30, 40, 261, 30));
        boardComboBox = new QComboBox(boardPage);
        boardComboBox.setObjectName("boardComboBox");
        boardComboBox.setGeometry(new QRect(30, 70, 251, 22));
        boardComboBox.activated.connect(this, "boardChanger()");
        boardCurrent = new QPushButton(boardPage);
        boardCurrent.setGeometry(new QRect(30, 110, 251, 251));
        boardCurrent.setStyleSheet(boardSheet);
        boardCurrent.setObjectName("boardCurrent");
        mainFrame.addWidget(boardPage);
        //iconPage
        iconPage = new QWidget();
        iconPage.setObjectName("iconPage");
        iconLabel = new QLabel(iconPage);
        iconLabel.setObjectName("iconLabel");
        iconLabel.setGeometry(new QRect(30, 40, 261, 30));
        //Colors/pieces
        //modern
        iconModern = new QPushButton(iconPage);
        iconModern.setGeometry(new QRect(40, 150, 75, 75));
        iconModern.setStyleSheet("border-image: url(bin/GUI/img_icons/pieces/00b_bishop.png); border: none;");
        iconModern.setObjectName("iconModern");
        iconModern.clicked.connect(this, "iconModernButton()");
        //classic
        iconClassic = new QPushButton(iconPage);
        iconClassic.setGeometry(new QRect(40, 230, 75, 75));
        iconClassic.setStyleSheet("border-image: url(bin/GUI/img_icons/pieces/01b_bishop.png); border: none;");
        iconClassic.setObjectName("iconClassic");
        iconClassic.clicked.connect(this, "iconClassicButton()");
        //plastic
        iconPlastic = new QPushButton(iconPage);
        iconPlastic.setGeometry(new QRect(115, 150, 75, 75));
        iconPlastic.setStyleSheet("border-image: url(bin/GUI/img_icons/pieces/02b_bishop.png); border: none;");
        iconPlastic.setObjectName("iconPlastic");
        iconPlastic.clicked.connect(this, "iconPlasticButton()");
        //wood
        iconWood = new QPushButton(iconPage);
        iconWood.setGeometry(new QRect(115, 225, 75, 75));
        iconWood.setStyleSheet("border-image: url(bin/GUI/img_icons/pieces/03b_bishop.png); border: none;");
        iconWood.setObjectName("iconWood");
        iconWood.clicked.connect(this, "iconWoodButton()");
        // old/retro
        iconOld = new QPushButton(iconPage);
        iconOld.setGeometry(new QRect(190, 150, 75, 75));
        iconOld.setStyleSheet("border-image: url(bin/GUI/img_icons/pieces/04b_bishop.png); border: none;");
        iconOld.setObjectName("iconOld");
        iconOld.clicked.connect(this, "iconOldButton()");
        //icon paper
        iconPaper = new QPushButton(iconPage);
        iconPaper.setGeometry(new QRect(190, 230, 75, 75));
        iconPaper.setStyleSheet("border-image: url(bin/GUI/img_icons/pieces/05b_bishop.png); border: none;");
        iconPaper.setObjectName("iconPaper");
        iconPaper.clicked.connect(this, "iconPaperButton()");
        //shows the current selected icon, updates when a iconButton isPressed
        iconCurrent = new QPushButton(iconPage);
        iconCurrent.setGeometry(new QRect(210, 40, 75, 75));
        iconCurrent.setStyleSheet(styleSheet);
        iconCurrent.setObjectName("iconCurrent");
        mainFrame.addWidget(iconPage);
        //hudPage
        hudPage = new QWidget();
        hudPage.setObjectName("page");
        redLabel = new QLabel(hudPage);
        redLabel.setObjectName("redLabel");
        redLabel.setGeometry(new QRect(320, 50, 81, 16));
        greenLabel = new QLabel(hudPage);
        greenLabel.setObjectName("greenLabel");
        greenLabel.setGeometry(new QRect(320, 80, 81, 16));
        blueLabel = new QLabel(hudPage);
        blueLabel.setObjectName("blueLabel");
        blueLabel.setGeometry(new QRect(320, 110, 81, 16));
        hudPageColorPrev = new QFrame(hudPage);				//previewBox
        hudPageColorPrev.setObjectName("hudPageColorPrev");
        hudPageColorPrev.setGeometry(new QRect(20,55,70,70));
        hudPageColorPrev.setAutoFillBackground(true);
        hudPageColorPrev.show();
        redSlider = new QSlider(Orientation.Horizontal,hudPage);
        redSlider.setObjectName("redSlider");
        redSlider.setRange(minScroll, maxScroll);
        redSlider.setGeometry(new QRect(130,50,180,20));
        redSlider.setValue(red);
        redSlider.sliderMoved.connect(this,"hudChanger()");
        redSlider.valueChanged.connect(this,"hudChanger()");
        greenSlider = new QSlider(Orientation.Horizontal,hudPage);
        greenSlider.setObjectName("greenSlider");
        greenSlider.setRange(minScroll, maxScroll);
        greenSlider.setGeometry(new QRect(130,80,180,20));
        greenSlider.setValue(green);
        greenSlider.sliderMoved.connect(this,"hudChanger()");
        greenSlider.valueChanged.connect(this,"hudChanger()");
        blueSlider = new QSlider(Orientation.Horizontal,hudPage);
        blueSlider.setObjectName("blueSlider");
        blueSlider.setRange(minScroll, maxScroll);
        blueSlider.setGeometry(new QRect(130,110,180,20));
        blueSlider.setValue(blue);
        blueSlider.sliderMoved.connect(this,"hudChanger()");
        blueSlider.valueChanged.connect(this,"hudChanger()");
        hudColorLab = new QLabel(hudPage);
        hudColorLab.setObjectName("hudcolor");
        hudColorLab.setGeometry(new QRect(20, 30, 81, 16));
        mainFrame.addWidget(hudPage);
        //gfxPage
        gfxPage = new QWidget();
        gfxPage.setObjectName("page_3");
        gfxLabel = new QLabel(gfxPage);
        gfxLabel.setObjectName("gfxLabel");
        gfxLabel.setGeometry(new QRect(30, 35, 251, 71));
        mainFrame.addWidget(gfxPage);
        //resolutionPage
        resolutionPage = new QWidget();
        resolutionPage.setObjectName("resolution");
        resComboBox = new QComboBox(resolutionPage);
        resComboBox.setObjectName("resComboBox");
        resComboBox.setGeometry(new QRect(30, 70, 251, 22));
        resComboBox.activated.connect(this, "resChanger()");
        resolutionLabel = new QLabel(resolutionPage);
        resolutionLabel.setObjectName("resolutionLabel");
        resolutionLabel.setGeometry(new QRect(30, 40, 261, 30));
        mainFrame.addWidget(resolutionPage);
        //general settings
        generalSettingsPage = new QWidget();
        generalSettingsPage.setObjectName("generalSettings");
        generalSettingsLab = new QLabel(generalSettingsPage);
        generalSettingsLab.setObjectName("generalSettingsLab");
        generalSettingsLab.setGeometry(new QRect(40, 20, 261, 21));
        gameModeLab = new QLabel(generalSettingsPage);
        gameModeLab.setObjectName("gameModeLab");
        gameModeLab.setGeometry(new QRect(50, 60, 261, 21));
        warningLab = new QLabel(generalSettingsPage);
        warningLab.setObjectName("Warning label");
        warningLab.setGeometry(new QRect(50,240,261,40));
        //RadioButtons for selecting the gameMode
        standardGM = new QRadioButton("Standard", generalSettingsPage);
        standardGM.setGeometry(new QRect(50,80,200,20));
        standardGM.clicked.connect(this, "gameModeChanger()");
        lockedGM = new QRadioButton("Pawns switch side", generalSettingsPage);
        lockedGM.setGeometry(new QRect(50,100,200,20));
        lockedGM.clicked.connect(this, "gameModeChanger()");
        noPawnsGM = new QRadioButton("No Pawns", generalSettingsPage);
        noPawnsGM.setGeometry(new QRect(50,120,200,20));
        noPawnsGM.clicked.connect(this, "gameModeChanger()");
        HaxxorGM = new QRadioButton("Some rules are missing", generalSettingsPage);
        HaxxorGM.setGeometry(new QRect(50,140,200,20));
        HaxxorGM.clicked.connect(this, "gameModeChanger()");
        quickChess = new QCheckBox("Quick Chess", generalSettingsPage);
        quickChess.setGeometry(new QRect(50,200,200,20));
        quickChess.clicked.connect(this, "timerChanger()");
        timer = new QCheckBox("Countdown",generalSettingsPage);
        timer.setGeometry(new QRect(50, 180, 200, 20));
        timer.clicked.connect(this, "timerChanger()");
        mainFrame.addWidget(generalSettingsPage);
        //Color/selectionPage
        selectionPage = new QWidget();
        selectionPage.setObjectName("Selection Color");
        selComboBox = new QComboBox(selectionPage);
        selComboBox.setObjectName("selComboBox");
        selComboBox.setGeometry(new QRect(30, 70, 251, 22));
        selComboBox.activated.connect(this, "selectionChanger()");
        selectionLabel = new QLabel(selectionPage);
        selectionLabel.setObjectName("selectionLabel");
        selectionLabel.setGeometry(new QRect(30, 40, 261, 30));
        mainFrame.addWidget(selectionPage);
        //cancelButton
        cancelButton = new QPushButton(mainWindow);
        cancelButton.setObjectName("cancelButton");
        cancelButton.setGeometry(new QRect(420, 380, 75, 23));
        cancelButton.clicked.connect(this, "cancelButton()");
        //okButton
        okButton = new QPushButton(mainWindow);
        okButton.setObjectName("okButton");
        okButton.setGeometry(new QRect(500, 380, 75, 23));
        okButton.clicked.connect(this, "okButton()");
        //treeWidget
        resetButton = new QPushButton(mainWindow);
        resetButton.setObjectName("Default");
        resetButton.setGeometry(new QRect(340, 380, 75, 23));
        resetButton.clicked.connect(this, "defaultButton()");
        treeWidget = new QTreeWidget(mainWindow);
        treeWidget.setObjectName("treeWidget");
        treeWidget.setGeometry(new QRect(10, 10, 161, 381));
        treeWidget.clicked.connect(this, "pageClicked()");
        retranslateUi(mainWindow);
        mainFrame.setCurrentIndex(3);
        mainWindow.connectSlotsByName();
        mainWindow.setModal(true);
        hudChanger();
        pageOnTop(generalSettingsPage);
    } 
    /**
     * Adds attributes to the different widgets
     * @param testGUI
     */
    private void retranslateUi(QDialog testGUI){
        testGUI.setWindowTitle("Options");
        testGUI.setWindowIcon(new QIcon("bin/GUI/img_icons/options.png"));
		testGUI.setMinimumSize(600, 420);
		testGUI.setMaximumSize(600, 420);
        //sets the text of the different labels
		colorsmain.setText("Here you can change the colors of the Board and HUD.");
        boardLabel.setText("Select a boardtype\nCurrently selected show under.");
		selectionLabel.setText("Select a selectioncolor:\nCurrently used is shown on top of list.");
		hudColorLab.setText("HUD color:");
		redLabel.setText("Red");
		greenLabel.setText("Green");
		blueLabel.setText("Blue");
		iconLabel.setText("Select a icontype:\nCurrent icon displayed to the right.");
		resolutionLabel.setText("Select a resolution:\nCurrent is shown on top of list.");
        generalSettingsLab.setText("Here you can change the basic settings of the game.");
      	gameModeLab.setText("Select the gamemode you want:");
        cancelButton.setText("Cancel");
        okButton.setText("OK");
        resetButton.setText("Default");
		warningLab.setText("WARNING! Changing game/timer mode will start\na new game.");
		gfxLabel.setText( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"+
		"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"+
		"p, li { white-space: pre-wrap; }\n"+
		"</style></head><body style=\" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"+
		"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">This is the basic GFX page, here you will be able</span></p>\n"+
		"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">to change the resolution and other graphics in </span></p>\n"+
		"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">the game for a better performance or a better</span></p>\n"+
		"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:8pt;\">looking game.</span></p></body></html>");
		//sets the list in QComboBox
		sortBoard();
		sortSelection();
		sortRes();
		sortGameMode();
		//checks if countDown or QuickMode is enabled and marks the QCheckBox if its true
		if(handler.getCountDown()){
			timer.setChecked(true);
		}
		if(handler.getQuick()){
			quickChess.setChecked(true);
		}
		//clears and adds widgets under into the QTreeWidget
        treeWidget.headerItem().setText(0,"Options");
        treeWidget.clear();
        colors = new QTreeWidgetItem(treeWidget);
        colors.setText(0,"Colors");
        selection = new QTreeWidgetItem(colors);
        selection.setText(0, "Selection color");
        board = new QTreeWidgetItem(colors);
        board.setText(0,"Board type");
        hud = new QTreeWidgetItem(colors);
        hud.setText(0,"HUD");
        iconType = new QTreeWidgetItem(colors);
        iconType.setText(0,"Pieces icons");
        gfx = new QTreeWidgetItem(treeWidget);
        gfx.setText(0,"GFX");
        resolution = new QTreeWidgetItem(gfx);
        resolution.setText(0,"Resolution");
        generalSettings = new QTreeWidgetItem(treeWidget);
        generalSettings.setText(0,"General Settings");
        
    }
    /**
     * Loads the default settings, from the defaultProperties file.
     */
    @SuppressWarnings("unused")
	private void defaultButton(){
    	handler.loadDefault();
    	gui.loadSettings();
    	gui.setSettings();
    	if(gui.isClock()){
    		gui.resumeClock();
    	}
    	mainWindow.close();
    }
    /**
     * Discards all the changes made and closes the option window.
     * Resumes the timer, if it is active.
     */
    @SuppressWarnings("unused")
	private void cancelButton(){
    	if(gui.isClock()){
    		gui.resumeClock();
    	}
    	mainWindow.close();
    }
    /**
	 * Checks if any changes have been made and applies the changes, if no changes
	 * is made, it loads the current values from appProperties file.
	 * Also checks if the gameMode has been changed and if timer has been
	 * changed. If any of these have been changed it start a new game.
	 */
	@SuppressWarnings("unused")
	private void okButton(){
		handler = new Settingshandler();
		handler.loadSettings();
		handler.setCountDown(timerChanger());
		handler.setGameMode(gameMode);
		handler.setBackgroundtype(boardColor);
		handler.setChessboardSizeX(newX);
		handler.setHudcolor(hudcolor);
		handler.setIcontype(iconTypeString);
		handler.setSelectcolor(selectionColor);
		handler.setClock(timer.isChecked());
		handler.setQuick(quickChess.isChecked());
		handler.saveSettings();
		gui.loadSettings();
		gui.setSettings();
		if(modeChange){
			gui.newGame();
		}
		if(newTimerChoice!=oldTimerChoice){
			gui.newGame();
		}
		if(newGame!=oldNewGame){
			gui.newGame();
		}
		if(gui.isClock()){
			gui.resumeClock();
		}
		mainWindow.close();
	}

	/**
     * Adds the items to the QComboBox(Resolution)
     */
    private void addRes(){
    	resComboBox.addItem("400x266");
        resComboBox.addItem("500x333");
        resComboBox.addItem("600x400");
        resComboBox.addItem("800x533");
        resComboBox.addItem("960x640");
        resComboBox.addItem("1280x853");
    }
    /**
     * Adds the items to the QComboBox(Selection)
     */
    private void addSel(){
    	selComboBox.addItem("Dark");//02 
    	selComboBox.addItem("Light");//01
        selComboBox.addItem("Solid");//03
        selComboBox.addItem("Transparent"); //00
    }
    /**
     * Adds the items to the QComboBox(board)
     */
    private void addBoardCol(){
        boardComboBox.addItem("Digital"); 			//00
        boardComboBox.addItem("Red/White wood");	//01
        boardComboBox.addItem("Bloody");			//02
        boardComboBox.addItem("Red/Black Marble");	//03
        boardComboBox.addItem("Light Wood");		//04
        boardComboBox.addItem("Brown/White Wood");	//05
        boardComboBox.addItem("Leather");			//06
        boardComboBox.addItem("White/Blue Marble");	//07
    }
    /**
     * sorts the QComboBox, clears the table, and the adds the selected item
     * then adds the addRes(). Finally removes the old selected value, so that 
     * no duplicates are exciting.
     */
    private void sortRes(){
    	resComboBox.clear();
    	if(handler.getChessboardSizeX()==400){
    		addRes();
    	}else if(handler.getChessboardSizeX()==500){
    		resComboBox.addItem("500x333");
    		addRes();
    		resComboBox.removeItem(2);
    		
    	}else if(handler.getChessboardSizeX()==600){
    		resComboBox.addItem("600x400");
    		addRes();
    		resComboBox.removeItem(3);
    	}else if(handler.getChessboardSizeX()==800){
            resComboBox.addItem("800x533");
            addRes();
            resComboBox.removeItem(4);
    		
    	}else if(handler.getChessboardSizeX()==960){
            resComboBox.addItem("960x640");
            addRes();
            resComboBox.removeItem(5);
            
    	}else if(handler.getChessboardSizeX()==1280){
            resComboBox.addItem("1280x853");
    		addRes();
    		resComboBox.removeItem(6);

    	}else{
    		addRes();
    	}
    }
    /**
     * checks what game mode that is selected and marks this one in the optionGUi/generalSettingsPage
     */
    private void sortGameMode(){
    	if(gameMode==0){standardGM.setChecked(true);}//Standard selected
    	else if(gameMode==1){lockedGM.setChecked(true);}//locked mode, pawns switch place, black switch with white
    	else if(gameMode==2){noPawnsGM.setChecked(true);}//no Pawns
    	else if(gameMode==3){HaxxorGM.setChecked(true);}//haxxor.mode
    	
    }
    /**
     * sorts the QComboBox so that the previous saved value is shown first in list.
     */
    
    private void sortSelection(){
    	selComboBox.clear();
    	if(handler.getSelectcolor().equals("00")){
    		selComboBox.addItem("Transparent");
    		addSel();
    		selComboBox.removeItem(4);
    	}else if(handler.getSelectcolor().equals("01")){
    		selComboBox.addItem("Light");
    		addSel();
    		selComboBox.removeItem(2);
    	}else if(handler.getSelectcolor().equals("02")){
    		addSel();
    	}else if(handler.getSelectcolor().equals("03")){
            selComboBox.addItem("Solid");
            addSel();
            selComboBox.removeItem(3);
    	}
    	else{
    		addSel();
    	}
    }
    /**
     * sorts the QComboBox so that the previous saved value is shown first in list.
     */
    private void sortBoard(){
    	boardComboBox.clear();
    	if(handler.getBackgroundtype().equalsIgnoreCase("00")){			 //digital
    		addBoardCol();
    	}else if(handler.getBackgroundtype().equalsIgnoreCase("01")){	 //red/white wood
    		boardComboBox.addItem("Red/White wood");
    		addBoardCol();
    		boardComboBox.removeItem(2);
    	}else if(handler.getBackgroundtype().equalsIgnoreCase("02")){	//Bloody
    		boardComboBox.addItem("Bloody");
    		addBoardCol();
    		boardComboBox.removeItem(3);
    	}else if(handler.getBackgroundtype().equalsIgnoreCase("03")){	//Red/Black Marble
    		boardComboBox.addItem("Red/Black Marble");
    		addBoardCol();
    		boardComboBox.removeItem(4);
    	}else if(handler.getBackgroundtype().equalsIgnoreCase("04")){	//Light Wood
    		boardComboBox.addItem("Light Wood");
    		addBoardCol();
    		boardComboBox.removeItem(5);
    	}else if(handler.getBackgroundtype().equalsIgnoreCase("05")){	//Brown/White Wood
    		boardComboBox.addItem("Brown/White Wood");
    		addBoardCol();
    		boardComboBox.removeItem(6);
    	}else if(handler.getBackgroundtype().equalsIgnoreCase("06")){	//Leather
    		boardComboBox.addItem("Leather"); 
    		addBoardCol();
    		boardComboBox.removeItem(7);
    	}else if(handler.getBackgroundtype().equalsIgnoreCase("07")){	//White/Blue Marble
    		boardComboBox.addItem("White/Blue Marble");
    		addBoardCol();
    		boardComboBox.removeItem(8);
    	}
    	else{
    		addBoardCol();
    	}
    }
   
    /**
	 * Splits up the color String and converts them to a number. These numbers are then saved
	 * so that they can be used to set the value on the slider bars.
	 * 
	 * Example:  Default starting position for the bar on the slider is 0.
	 * []-----------
	 * when redSlider.setValue(red)is used, the bar on the slider to the value of red.
	 *  ------[]-----
	 *
	 */
	private void sortColors(){
		String[] ColorValues = (handler.getHudcolor().split(","));
		red= Integer.parseInt(ColorValues[0]);
		green=Integer.parseInt(ColorValues[1]);
		blue=Integer.parseInt(ColorValues[2]);
		
	}
	/**
	 * Changes the gameMode of the game,the gameMode is set true if the value is changed.
	 * This way we can know when the gameMode has been changed and a new game will start.
	 */
	@SuppressWarnings("unused")
	private void gameModeChanger(){
		timer.setEnabled(true);
		if(standardGM.isChecked()){
			gameMode=0;
			modeChange=true;
		}
		else if(lockedGM.isChecked()){
			gameMode=1;
			modeChange=true;
		}
		else if(noPawnsGM.isChecked()){
			gameMode=2;
			modeChange=true;
		}
		else if(HaxxorGM.isChecked()){
			gameMode=3;
			modeChange=true;
		}
	}

	/**
     * Changes the resolution on the chess frame
     */
    @SuppressWarnings("unused")
	private void resChanger(){
    	handler.loadSettings();
    	if(resComboBox.currentText().equalsIgnoreCase("400x266")){newX=400;}
    	else if(resComboBox.currentText().equalsIgnoreCase("500x333")){newX=500;}
    	else if(resComboBox.currentText().equalsIgnoreCase("600x400")){newX=600;}
    	else if(resComboBox.currentText().equalsIgnoreCase("800x533")){newX=800;}
    	else if(resComboBox.currentText().equalsIgnoreCase("960x640")){newX=960;}
    	else if(resComboBox.currentText().equalsIgnoreCase("1280x853")){newX=1280;}
    	else{newX=handler.getChessboardSizeX();}
    }
    /**
	 * Changes the color of the HUD by taking the color from the sliders, the color is saved as
	 * a String. This String is saved to the appProperties file with this format(Red,Green,Blue,Alpha) where
	 * Alpha is a set value of 255.
	 */
	private void hudChanger(){
		hudcolor =""+redSlider.value()+","+greenSlider.value()+","+blueSlider.value()+","+255;
		pal = new QPalette();
	   	pal.setColor(ColorRole.Window, new QColor(redSlider.value(),greenSlider.value(),blueSlider.value(),255));
	   	hudPage.setPalette(pal);
	}

	/**
     * Changes the color of the selection:
     * 00 = Transparent;
     * 01 = Light;
     * 02 = Dark;
     * 03 = Solid;
     */
	 @SuppressWarnings("unused")
    private void selectionChanger(){
    	if(selComboBox.currentText().equalsIgnoreCase("Solid")){selectionColor="03";}//solid
    	else if(selComboBox.currentText().equalsIgnoreCase("Dark")){selectionColor="02";}//dark
    	else if(selComboBox.currentText().equalsIgnoreCase("Light")){selectionColor="01";}//light
    	else if(selComboBox.currentText().equalsIgnoreCase("Transparent")){selectionColor="00";}//Transparent
    	else{selectionColor=handler.getSelectcolor();}
    }
  
    /**
     * Changes the color of the board:
     * 00 = Digital;
     * 01 = Red/White wood;
     * 02 = Bloody;
     * 03 = Red/Black Marble;
     * 04 = Light Wood;
     * 05 = Brown/White Wood;
     * 06 = Leather;
     * 07 = White/Blue Marble;
     */
	 @SuppressWarnings("unused")
    private void boardChanger(){
    	if(boardComboBox.currentText().equalsIgnoreCase("Digital")){boardColor="00";showCurrentBoard();}
    	else if(boardComboBox.currentText().equalsIgnoreCase("Red/White wood")){boardColor="01";showCurrentBoard();}
    	else if(boardComboBox.currentText().equalsIgnoreCase("Bloody")){boardColor="02";showCurrentBoard();}
    	else if(boardComboBox.currentText().equalsIgnoreCase("Red/Black Marble")){boardColor="03";showCurrentBoard();}
    	else if(boardComboBox.currentText().equalsIgnoreCase("Light Wood")){boardColor="04";showCurrentBoard();}
    	else if(boardComboBox.currentText().equalsIgnoreCase("Brown/White Wood")){boardColor="05";showCurrentBoard();}
    	else if(boardComboBox.currentText().equalsIgnoreCase("Leather")){boardColor="06";showCurrentBoard();}
    	else if(boardComboBox.currentText().equalsIgnoreCase("White/Blue Marble")){boardColor="07";showCurrentBoard();}
    	else{boardColor=handler.getBackgroundtype();}
    }
    /**
     * Handles the CheckBox If the box is checked newTimerChoice
     * will be changed to true, else if will changed to false.
     * @return
     */
    private boolean timerChanger(){
    	oldTimerChoice=handler.getCountDown();
    	if(timer.isChecked()){
    		newTimerChoice=true;
    		if(quickChess.isChecked()){
    			newGame=true;
    			return true;
    		}
    		else return true;
    	}
    	else{
    		newTimerChoice=false;
    		return false;
    	}
    }
    
    /**
     * Changes the iconTypeString to Modern/Classic/Plastic/Wood/Old when the appropriate button is clicked. 
     */
    @SuppressWarnings("unused") private void iconModernButton(){iconTypeString="00";showCurrentIcon();}
    @SuppressWarnings("unused") private void iconClassicButton(){iconTypeString="01";showCurrentIcon();}
    @SuppressWarnings("unused") private void iconPlasticButton(){iconTypeString="02";showCurrentIcon();}
    @SuppressWarnings("unused") private void iconWoodButton(){iconTypeString="03";showCurrentIcon();}
    @SuppressWarnings("unused") private void iconOldButton(){iconTypeString="04";showCurrentIcon();}
    @SuppressWarnings("unused") private void iconPaperButton(){iconTypeString="05";showCurrentIcon();}
    /**
     *Updates the current selected piece from the list, and displays the appropriate image
     */
    private void showCurrentIcon(){
    	styleSheet ="border-image: url(bin/GUI/img_icons/pieces/"+iconTypeString+"b_bishop.png); border: none;";
        iconCurrent.setStyleSheet(styleSheet);
        iconCurrent.update();
    	iconPage.update();	
    }
    /**
     * Updates the current selected piece from the list, and displays the appropriate image
     */
    private void showCurrentBoard(){
    	boardSheet  = "border-image: url(bin/GUI/img_icons/background"+boardColor+".png); border: none;";
    	boardCurrent.setStyleSheet(boardSheet);
        boardCurrent.update();
    	boardPage.update();
    }
    /**
     * this is the listener for the widget tree. isSelected returns value true if its
     * selected. Will show the page widget that is selected.
     */
    @SuppressWarnings("unused")
    private void pageClicked(){
    	if(colors.isSelected()){pageOnTop(colorsMainPage);}
         else if(selection.isSelected()){pageOnTop(selectionPage);}
         else if(hud.isSelected()){pageOnTop(hudPage); hudPageColorPrev.show();}
         else if(board.isSelected()){pageOnTop(boardPage);}
         else if(gfx.isSelected()){pageOnTop(gfxPage);}
		 else if(resolution.isSelected()){pageOnTop(resolutionPage);}
		 else if(generalSettings.isSelected()){pageOnTop(generalSettingsPage);}
		 else if(iconType.isSelected()){pageOnTop(iconPage);}
    }
    /**
     * hides all pages in the QWidget and sets the parameter page visible
     * @param pageName
     */
    private void pageOnTop(QWidget pageName){
    	colorsMainPage.hide();
    	generalSettingsPage.hide();
    	hudPage.hide();
    	selectionPage.hide();
    	gfxPage.hide();
    	resolutionPage.hide();
    	generalSettingsPage.hide();
    	iconPage.hide();
    	boardPage.hide();
    	pageName.show();
	}
}
